# -*- coding: utf-8 -*-

import views.notes
import models.notes

class NotesController():

    # @type view views.books.BooksView
    view = None
    model = None
    activeNoteId = None
    booksController = None
    activeBookId = None
    currentBookId = None
    selectedBookId = None

    def __init__(self, frame, booksController):
        self.view = views.notes.NotesView(self, frame)
        self.model = models.notes.NotesModel(booksController.model)
        self.booksController = booksController

    def initAction(self):
        self.view.hideNotePanel()


    def newAction(self, event):
        self.activeNoteId = None
        self.view.resetNotePanel()
        self.view.showNotePanel()
        self.view.resetNoteBooksPanel()
        self.view.showNoteBooksPanel()
        self.view.focusNotePanel()
        self.setActiveBookId(self.activeBookId)
        self.setCurrentBookId(None)
        
    def saveAction(self, event):
        noteData = self.view.getNoteBasicData()

        if self.activeNoteId is not None:
            noteData["id"] = self.activeNoteId
            noteData["bookId"] = self.activeBookId
        else:
            assignType = self.view.getBookAssignType()

            if assignType is "none":
                id = self.model.getDefaultBookId()
                if id is None:
                    self.booksController.createDefaultBookAction()
                noteData["bookId"] = self.model.getDefaultBookId()
            elif assignType is "active":
                noteData["bookId"] = self.activeBookId
            else:
                selBookId = self.booksController.view.getSelectedNoteBookId()
                if selBookId is not None:
                    noteData["bookId"] = selBookId
                else:
                    self.view.showError(u"Nebyla zvolena publikace!")
                    return


        if len(noteData["header"]) == 0:
            self.view.showError(u"Název poznámky nebyl vyplněn")
            return

        self.view.setStatus(u"Ukládám poznámku...")
        id = self.model.saveNote(noteData)
        if id is not False:
            noteData["id"] = id
            self.view.setStatus(u"Hotovo")
            self.refreshNoteListsAction()
            self.editAction(noteData["id"], noteData["bookId"])
        else:
            self.view.setStatus(u"Při ukládání došlo k chybě")
            self.view.showError(u"Při ukládání došlo k chybě")

    def refreshNoteListsAction(self, event = None, tags = None):
        self.view.setStatus(u"Obnovuji seznam poznámek...")
        notes = {}
        if self.activeBookId is not None:
            notes = self.model.getNotesByBookId(self.activeBookId)
        self.view.updateBookNotesList(notes)
        self.updateListAction()
        self.view.setStatus(u"Hotovo")

    def editAction(self, noteID, bookId = None):
        if bookId is not None:
            note = self.model.getBookNoteById(bookId, noteID)
        else:
            if self.activeBookId is not None:
                note = self.model.getBookNoteById(self.activeBookId, noteID)
            else:
                note = self.model.getBookNoteById(self.model.getDefaultBookId(), noteID)
        if note is not None:
            self.activeBookId = note["bookId"]
            self.view.resetNotePanel()
            self.view.setActiveNote(note)
            self.view.hideNoteBooksPanel()
            self.view.showNotePanel(note["header"])
            self.view.focusNotePanel()
            self.activeNoteId = noteID
            self.setActiveBookId(self.activeBookId)

            self.setCurrentBookId(note["bookId"])

    def setActiveBookId(self, bookId):
        self.activeBookId = bookId
        if bookId is not None:
            book = self.booksController.model.getBookById(bookId)
        else:
            book = None
        self.view.setActiveBook(book)

    def setCurrentBookId(self, bookId):
        self.currentBookId = bookId
        if bookId is not None:
            book = self.booksController.model.getBookById(bookId)
        else:
            book = None

        self.view.setCurrentBook(book)

    def syncAction(self):
        self.updateListAction()
        self.view.setStatus("Hotovo")

    def updateListAction(self, event = None):
        self.view.updateNotesPagination(self.model.getNewestNotesCount())
        pagination = self.view.getNotesPagination()
        notes = self.model.getNewestNotes(pagination["offset"], pagination["limit"])
        self.view.updateNotesList(notes)

    def onHyperlinkCurrent(self):
        if self.activeNoteId is None:
            self.view.focusNoteBooksPanel()
        else:
            self.booksController.editAction(self.currentBookId)